package com.qdu.study_app.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.qdu.study_app.R;
import com.qdu.study_app.bean.Notes;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobArticle;
import cn.bmob.v3.BmobUser;

public class MessageListAdapter extends BaseAdapter {

    private Context context;
    private List<Notes> articleList = new ArrayList<>();
    public MessageListAdapter(Context context) {
        this.context = context;
    }

    public List<Notes> getArticleList() {
        return articleList;
    }

    public void setArticleList(List<Notes> articleList) {
        this.articleList = articleList;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return articleList.size();
    }

    @Override
    public Object getItem(int position) {
        return articleList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /*
    * 重点实现getView方法
    * 通过请求数据，动态地更新ViewUI
    * */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            // 生成View
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.message_list_cell,null);
        }
        // 这样写比较耗资源
        //TextView tvCellLabel = (TextView) convertView.findViewById(R.id.tvCellLable);
        TextView user_id = convertView.findViewById(R.id.msg_user_id);
        TextView msg_title = convertView.findViewById(R.id.tvCellLable);
        TextView msg_s_content = convertView.findViewById(R.id.msg_s_content);
        TextView msg_create_time = convertView.findViewById(R.id.msg_create_time);

        // 获取数据
        Notes message = (Notes) getItem(position);

        user_id.setText(message.getUser_id()+"打卡：");
        msg_title.setText(message.getTitle());
        msg_s_content.setText(message.getContent());
        msg_create_time.setText(message.getCreatedAt());
        return convertView;
    }

    public void addAll(List<Notes> data){
        articleList.addAll(data);
        // 数据改变时，列表更新
        notifyDataSetChanged();
    }
    public void clear(){
        articleList.clear();
        notifyDataSetChanged();
    }

    /*private static class ListCell{
        private TextView  tvCellLabel;

        public ListCell(TextView tvCellLabel) {
            this.tvCellLabel = tvCellLabel;
        }

        public TextView getTvCellLabel() {
            return tvCellLabel;
        }

        public void setTvCellLabel(TextView tvCellLabel) {
            this.tvCellLabel = tvCellLabel;
        }
    }*/
}
